package com.wfh.easychat.websocket;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;

/**
 * @Title: TimeOutHandler
 * @Author wangfenghuan
 * @Package com.wfh.easychat.websocket
 * @Date 2025/10/5 13:38
 * @description: 超时处理器
 */
@Slf4j
public class TimeOutHandler extends ChannelDuplexHandler {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent event){
            if (event.state() == IdleState.READER_IDLE){
                log.info("心跳超时");
            } else if (event.state() == IdleState.READER_IDLE) {
                ctx.writeAndFlush("heart");
            }
        }
    }
}
